<?php
namespace Popshouse\AdminBundle\Controller;

use Popshouse\SiteBundle\Entity\Pageimg;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Popshouse\SiteBundle\Entity\Background;
use Popshouse\AdminBundle\Form\BackgroundForm;

use Popshouse\AdminBundle\Helper\Arr;
use Popshouse\AdminBundle\Helper\Image;


class ImageController extends Controller
{

    public function indexAction()
    {
    	$em = $this->get('doctrine.orm.entity_manager');
    	
        $query = $em->createQuery("SELECT i FROM Popshouse\SiteBundle\Entity\Pageimg i ORDER BY i.sort ASC");
		$images = $query->getResult();
        return $this->render('PopshouseAdminBundle:Page:images.html.twig', array('images' => $images, 'active' => 'page'));
    }
    
  
 	public function sortAction()
    {
    	$em = $this->get('doctrine.orm.entity_manager');
    	
        $query = $em->createQuery("SELECT i FROM Popshouse\SiteBundle\Entity\Pageimg i ORDER BY i.sort ASC");
		$images = $query->getResult();
		
		$order = $this->get('request')->request->get('order');
	
		if(!empty($order)){
			$orders = Arr::decodeJSArray($order);
			foreach($images as $image){
				$image->setSort(array_search($image->getId(), $orders));
			}
			$em->flush();
			return new Response(json_encode(array('message'=>'Volgorde Opgeslaan')));
		}
		
		return $this->render('PopshouseAdminBundle:Page:sort.html.twig', array('images' => $images, 'active' => 'page'));
        //return array('images' => $images);
    }
    

    public function createAction(){
    	$img = new Pageimg();
    	
    	$form = $this->get('form.factory')->createNamed(new BackgroundForm(),'image',$img);
        $request = $this->get('request');
	    if ($request->getMethod() == 'POST') {
			$form->bindRequest($request);
	        if ($form->isValid()) {
		  		$em = $this->get('doctrine.orm.entity_manager');
		        $em->persist($img);
		        $em->flush();
	            
		        $image = new Image(PUBLIC_PATH . $form->get('afbeelding')->getData());
	        	$image->save($img->getImagePath());
	        	$image->resize(50,50,Image::HEIGHT);
	        	$image->crop(50,50);
	        	$image->save($img->getImagePath(true));
	        	
	        	unlink(PUBLIC_PATH . $form->get('afbeelding')->getData());
		        
		        $this->get('session')->setFlash('notice', 'Afbeelding toegevoegd!');
	           	return new RedirectResponse($this->generateUrl('image'));
	        }
        }

       return $this->render('PopshouseAdminBundle:Page:addImage.html.twig', array('form' => $form->createView(), 'active' => 'page'));
    }
    
    public function deleteAction($id = 0){
		$em = $this->get('doctrine.orm.entity_manager');
        $img = $em->find('Popshouse\SiteBundle\Entity\Pageimg', $id);
        if($img === null || !$img->getId())
           throw new \Exception("Deze afbeelding werd niet gevonden");
         	
        
        $em->remove($img);
        $em->flush();
        
        return new Response(json_encode(array('message'=>'Verwijdert')));
        
    }
    
    
 	public function editAction($id = 0){
    	
    	if($id != 0){
    		$em = $this->get('doctrine.orm.entity_manager');
    		$image = $em->find('Popshouse\SiteBundle\Entity\Pageimg', $id);
    		if($image===null || !$image->getId())
    			throw new \Exception("Deze afbeelding werd niet gevonden");
    			
    		$image->loadTranslation();
    	}else{
    		return new RedirectResponse($this->generateUrl('image_new'));
    	}
    	

        $form = $this->get('form.factory')->createNamed(new BackgroundForm(),'image',$image);
        $request = $this->get('request');
	    if ($request->getMethod() == 'POST') {
	        $form->bindRequest($request);
	        if ($form->isValid()) {
	        	
		  		$em = $this->get('doctrine.orm.entity_manager');
		        $em->flush();
	            
		        $this->get('session')->setFlash('notice', 'Afbeelding gewijzigd!');
	           	return new RedirectResponse($this->generateUrl('image'));
	        }
	    }
        return $this->render('PopshouseAdminBundle:Page:images.html.twig', array('form' => $form->createView(), 'image'=>$image, 'active' => 'page'));
    }
}
